package cn.iocoder.yudao.module.system.service.tiktok.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.dal.dataobject.tiktok.TiktokUser;
import cn.iocoder.yudao.module.system.dal.dto.tiktok.TiktokUserDto;
import cn.iocoder.yudao.module.system.dal.mysql.tiktok.TiktokUserMapper;
import cn.iocoder.yudao.module.system.service.tiktok.TiktokUserService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

@Slf4j
@Service
public class TiktokUserServiceImpl extends ServiceImpl<TiktokUserMapper, TiktokUser> implements TiktokUserService {

    /**
     * 查询tiktok社交信息
     *
     * @return
     */
    @Override
    public TiktokUserDto getTiktokUserInfo() {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        if (Objects.isNull(loginUserId))
            return null;
        TiktokUser one = getOne(Wrappers.lambdaQuery(TiktokUser.class)
                .eq(TiktokUser::getUserId, loginUserId)
                .orderByDesc(TiktokUser::getId)
                .last("limit 1"));
        if (Objects.nonNull(one)) {
            TiktokUserDto tiktokUserDto = BeanUtil.copyProperties(one, TiktokUserDto.class);
            tiktokUserDto.setAccessToken(null).setRefreshToken(null).setId(null);
            return tiktokUserDto;
        }
        return null;
    }
}